<template>
<el-dialog :title="!dataForm.id ? '新建' :'编辑'"
           :close-on-click-modal="false" append-to-body
           :visible.sync="visible" class="JNPF-dialog JNPF-dialog_center" lock-scroll
           width="600px">
<el-row :gutter="15" class="">
<el-form ref="elForm" :model="dataForm" :rules="rules" size="small" label-width="100px" label-position="left" >
    <template v-if="!loading">
        <el-col   :span="24" >
        <el-card class="mb-20" shadow ="always"  header ="新建巡检计划">
            <el-col :span="6"  >
                <el-form-item  label="项目ID"
 prop="projectId" >
                    <el-input   v-model="dataForm.projectId"
 placeholder="请输入"  clearable  :style='{"width":"100%"}'>

                </el-input>
                </el-form-item>
            </el-col>
            <el-col :span="6"  >
                <el-form-item  label="计划名称"
 prop="planName" >
                    <el-input   v-model="dataForm.planName"
 placeholder="请输入"  clearable  :style='{"width":"100%"}'>

                </el-input>
                </el-form-item>
            </el-col>
            <el-col :span="6"  >
                <el-form-item  label="生效日期"
 prop="effectDate" >
                    <el-date-picker   v-model="dataForm.effectDate"
 placeholder="请选择"  clearable  :style='{"width":"100%"}' type="date"  format="yyyy-MM-dd"  value-format="timestamp" >

                </el-date-picker>
                </el-form-item>
            </el-col>
            <el-col :span="6"  >
                <el-form-item  label="失效日期"
 prop="lapseData" >
                    <el-date-picker   v-model="dataForm.lapseData"
 placeholder="请选择"  clearable  :style='{"width":"100%"}' type="date"  format="yyyy-MM-dd"  value-format="timestamp" >

                </el-date-picker>
                </el-form-item>
            </el-col>
            <el-col :span="6"  >
                <el-form-item  label="巡检周期"
 prop="cycle" >
                    <el-radio-group   v-model="dataForm.cycle"
 size="small" >
                            <el-radio-button  v-for="(item, index) in cycleOptions" :key="index" :label="item.id" :disabled="item.disabled" >
                                {{item.fullName}}
                            </el-radio-button >

                </el-radio-group>
                </el-form-item>
            </el-col>
            <el-col :span="6"  >
                <el-form-item  label="间隔天数"
 prop="generateConfig" >
                    <el-input   v-model="dataForm.generateConfig"
 placeholder="请输入"  clearable  :style='{"width":"100%"}'>

                </el-input>
                </el-form-item>
            </el-col>
            <el-col :span="6"  >
                <el-form-item  label="执行人"
 prop="executorId" >
                    <user-select   v-model="dataForm.executorId"
 placeholder="请选择"  clearable  :multiple="true" >

                </user-select>
                </el-form-item>
            </el-col>
        </el-card>
        </el-col>
        <el-col :span="6">
        <el-row :gutter="15">
            <el-col :span="24"  >
                <el-form-item  label="修改时间"
 prop="lastmodifytime" >
                    <popupSelect   v-model="dataForm.lastmodifytime"
 placeholder="请选择"  clearable                         field="lastmodifytime" interfaceId="612769091110831365" :columnOptions="lastmodifytimecolumnOptions" propsValue="id" relationField="assetName" popupType="dialog"
 popupTitle="选择数据"  popupWidth="800px"
 hasPage :pageSize="20"   >

                </popupSelect>
                </el-form-item>
            </el-col>
        </el-row>
        </el-col>
    </template>
</el-form>
    <SelectDialog v-if="selectDialogVisible" :config="currTableConf" :formData="dataForm"
              ref="selectDialog" @select="addForSelect" @close="selectDialogVisible=false"/>
    </el-row>
    <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false"> 取 消</el-button>
        <el-button type="primary" @click="dataFormSubmit()" :loading="btnLoading"> 确 定</el-button>
    </span>
    </el-dialog>
</template>
<script>
    import request from '@/utils/request'
    import { getDataInterfaceRes } from '@/api/systemData/dataInterface'
    import { getDictionaryDataSelector } from '@/api/systemData/dictionary'

    export default {
        components: {},
        props: [],
        data() {
            return {
                visible: false,
                loading: false,
                btnLoading: false,
                selectDialogVisible: false,
                currTableConf:{},
                addTableConf:{
            },
                tableRows:{
                },
            currVmodel:"",
            dataForm: {
                                    projectId : '',
                                    planName : '',
                                    effectDate : '',
                                    lapseData : '',
                                cycle : "天",
                                    generateConfig : '',
                                executorId : [],
                                lastmodifytime : "",
            },
            rules:
            {
                            projectId: [
                                {
                                    required: true,
                                    message: '请输入',
                                    trigger: 'blur'
                                },
                        ],
                            planName: [
                                {
                                    required: true,
                                    message: '请输入',
                                    trigger: 'blur'
                                },
                        ],
                            effectDate: [
                                {
                                    required: true,
                                    message: '请选择',
                                    trigger: 'change'
                                },
                        ],
                            lapseData: [
                                {
                                    required: true,
                                    message: '请选择',
                                    trigger: 'change'
                                },
                        ],
                            cycle: [
                                {
                                    required: true,
                                    message: '请至少选择一个',
                                    trigger: 'change'
                                },
                        ],
                            generateConfig: [
                                {
                                    required: true,
                                    message: '请输入',
                                    trigger: 'blur'
                                },
                        ],
                            executorId: [
                                {
                                    required: true,
                                    message: '请至少选择一个',
                                    trigger: 'click'
                                },
                        ],



            },
                        cycleOptions:[{"fullName":"天","id":"天"},{"fullName":"周","id":"周"},{"fullName":"月","id":"月"},{"fullName":"年","id":"年"}],
                    lastmodifytimecolumnOptions:[],

        }
        },
        computed: {},
        watch: {},
        created() {
        },
        mounted() {},
        methods: {
            clearData(){
                               this.dataForm.projectId = '';
                               this.dataForm.planName = '';
                               this.dataForm.effectDate = '';
                               this.dataForm.lapseData = '';
                               this.dataForm.cycle = "天";
                               this.dataForm.generateConfig = '';
                                 this.dataForm.executorId = [];
                               this.dataForm.lastmodifytime = "";
            },
            init(id) {
                this.dataForm.id = id || 0;
                this.visible = true;
                this.$nextTick(() => {
                    this.$refs['elForm'].resetFields();
                    if(this.dataForm.id){
                        this.loading = true
                        request({
                            url: '/api/example/ZcglInspectionPlan/'+this.dataForm.id,
                            method: 'get'
                        }).then(res => {
                            this.dataInfo(res.data)
                            this.loading = false
                        });
                    }else{
                       this.clearData()
                    }
                });
                this.$store.commit('generator/UPDATE_RELATION_DATA', {})
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['elForm'].validate((valid) => {
                    if (valid) {
                            this.request()
                    }
                })
            },
            request() {
                this.btnLoading = true
                let _data =this.dataList()
                if (!this.dataForm.id) {
                    request({
                        url: '/api/example/ZcglInspectionPlan',
                        method: 'post',
                        data: _data
                    }).then((res) => {
                        this.$message({
                            message: res.msg,
                            type: 'success',
                            duration: 1000,
                            onClose: () => {
                                this.visible = false
                                this.btnLoading = false
                                this.$emit('refresh', true)
                            }
                        })
                    }).catch(()=>{  this.btnLoading = false  })
                }else{
                    request({
                        url: '/api/example/ZcglInspectionPlan/'+this.dataForm.id,
                        method: 'PUT',
                        data: _data
                    }).then((res) => {
                        this.$message({
                            message: res.msg,
                            type: 'success',
                            duration: 1000,
                            onClose: () => {
                                this.visible = false
                                this.btnLoading = false
                                this.$emit('refresh', true)
                            }
                        })
                    }).catch(()=>{  this.btnLoading = false  })
                }
            },
            openSelectDialog(key) {
                this.currTableConf=this.addTableConf[key]
                this.currVmodel=key
                this.selectDialogVisible = true
                this.$nextTick(() => {
                    this.$refs.selectDialog.init()
                })
            },
            addForSelect(data) {
                for (let i = 0; i < data.length; i++) {
                    let item={...this.tableRows[this.currVmodel],...data[i]}
                    this.dataForm[this.currVmodel].push(item)
                }
            },
            dataList(){
                var _data = JSON.parse(JSON.stringify(this.dataForm));
            _data.executorId = Array.isArray(_data.executorId)? JSON.stringify(_data.executorId):'[]'
                return _data;
            },
            dataInfo(dataAll){
                let _dataAll =dataAll
            _dataAll.executorId = _dataAll.executorId ?JSON.parse(_dataAll.executorId):[]
                this.dataForm = _dataAll
            },
        },
    }

</script>
